package araujoCoronelVillamayor.plantasVsZombies.uiWicket;

import araujoCoronelVillamayor.plantasVsZombies.uiWicket.layouts.LayoutPrincipal
import org.apache.wicket.markup.html.basic.Label
import org.apache.wicket.model.CompoundPropertyModel
import org.uqbar.wicket.xtend.WicketExtensionFactoryMethods
import araujoCoronelVillamayor.plantasVsZombies.domain.Fila
import org.apache.wicket.markup.html.form.DropDownChoice
import araujoCoronelVillamayor.plantasVsZombies.appModelsArena.SesionAppModel
import araujoCoronelVillamayor.plantasVsZombies.domain.Planta
import org.apache.wicket.markup.html.form.Form
import org.uqbar.wicket.xtend.XButton
import org.apache.wicket.markup.html.panel.FeedbackPanel
import org.uqbar.wicket.xtend.XAttributeModifier
import org.uqbar.commons.model.UserException
import org.apache.wicket.model.PropertyModel

class PlantacionPage extends LayoutPrincipal {
	extension WicketExtensionFactoryMethods = new WicketExtensionFactoryMethods
	
	private final JardinPage mainPage
	String filaId
	String tipoFila
	String mensajeOk
	
	new(Fila filaParametro, SesionAppModel sesionAppModelParametro, JardinPage mainPage) {
		super();
		
		this.mainPage = mainPage
		defaultModel = new CompoundPropertyModel(sesionAppModelParametro)
		
		sesion.filaSeleccionada = filaParametro
		
        
		val plantarForm = new Form<SesionAppModel>("nuevaPlantaForm", sesion.asCompoundModel)
		
														 
		plantarForm.addChild(new DropDownChoice<Planta>("plantaSeleccionada") => [
			choices = loadableModel([| sesion.filaSeleccionada.plantasDelMismoTipo ])
			choiceRenderer = choiceRenderer([Planta p| p.nombre ])
			
			])
        
        plantarForm.addChild(new XButton("botonPlantar") => [
			onClick = [| plantar() ]
        ])

        plantarForm.addChild(new XButton("volverJardin") => [
			onClick = [| volver() ]
        ])

		tipoFila = sesion.filaSeleccionada.tipo.toLowerCase
		plantarForm.addChild(new Label("tipoFila", this.tipoFila))
		this.addChild(plantarForm)
		filaId = (sesion.jardin.filas.indexOf(sesion.filaSeleccionada) + 1).toString + " (" + tipoFila + ")"
		this.addChild(new Label("filaId", this.filaId))
		this.addChild(new Label("lugaresLibres", new PropertyModel(sesion.filaSeleccionada, "lugaresLibres")));
		this.addChild(new Label("mensajeOk", new PropertyModel(this, "mensajeOk")));
        this.addChild(new FeedbackPanel("feedbackPanel"))

		val panelEstadoJardin = new EstadoJardinPanel("panelEstadoJardin", sesion);
		this.addChild(panelEstadoJardin);

        
    }
    
    def plantar() {
    	
    	mensajeOk = ""
    	
    	try {
	    	sesion.poner()
	    	mensajeOk = "Se plantó " + sesion.plantaSeleccionada.nombre + "."
    	}
    	catch (UserException e) {
			error(e.getMessage())
		}
    }
    
	def volver() {
		mainPage.refreshFilas()
		responsePage = mainPage
	}
   
 	def getSesion() {
		this.getDefaultModelObject() as SesionAppModel;
	}
}
